<?php
/**
 * @ Fmash Top System
 * @ version: 3.0
 * @ author: i.coffee.smile@mail.com
 * @ date:2011-1-27
 * @ file:function.php $15
 */


//更新数据
function update()
{
	$sql = "UPDATE `show` SET winper = win /(win + lose) ";
	$result = mysql_query($sql);	
	$sql_charm = "UPDATE `show` SET charm = win * win /(win + lose) + flower * 0.5";	
	$result_charm = mysql_query($sql_charm);
}


//加密ID
function encode_id($id)
{
	return base64_encode($id * '220301' + '21215');
}

//解密ID
function decode_id($en_id)
{
	return (base64_decode($en_id) - '21215')/'220301';
}

//防作弊密钥
function encode_url_key()
{
	return base64_encode(base64_encode(time()));	
}

//解防作弊密钥
function decode_url_key($key)
{
	return base64_decode(base64_decode($key));
}

//取头像地址
function get_avatar($uid,$size)
{
	$domain = "http://www.fmash.com/";	
	echo $domain.'uc_server/avatar.php?uid='.$uid.'&size='.$size;
}

//算到现在的时间
function commentday($time)
{
	$fromtime = time() - $time;
	if($fromtime> 600){
		$day = "10分钟前";
		if($fromtime > 1800){
			$day = "半个小时前";
			if($fromtime >3600){
				$day = "1个小时前";
				if($fromtime > 86400){
					$day = "1天前";
					if($fromtime >172800){
						$day = "2天前";
						if($fromtime > 259200){
							$day = "3天前";
							if($fromtime > 500000){
								$day = "很久之前";
							}
						}
					}
				}
			}
		}
	}else{
		$day = "刚刚";
	}
	return $day;
}

function time2Units($commenttime)
{
	$time = time() - $commenttime;
	$year = floor($time / 60 / 60 / 24 / 365);
	$time -= $year * 60 * 60 * 24 * 365;
	$month = floor($time / 60 / 60 / 24 / 30);
	$time -= $month * 60 * 60 * 24 * 30;
	$week = floor($time / 60 / 60 / 24 / 7);
	$time -= $week * 60 * 60 * 24 * 7;
	$day = floor($time / 60 / 60 / 24);
	$time -= $day * 60 * 60 * 24;
	$hour = floor($time / 60 / 60);
	$time -= $hour * 60 * 60;
	$minute = floor($time / 60);
	$time -= $minute * 60;
	$second = $time;
	$elapse = '';
	$unitArr = array('年' =>'year', '个月'=>'month', '周'=>'week', '天'=>'day',
	'小时'=>'hour', '分钟'=>'minute', '秒'=>'second');
	foreach ( $unitArr as $cn => $u )
	{
		if ( $$u > 0 )
		{
		$elapse = $$u . $cn;
		break;
		}
	}
	$echo = '发表于'.$elapse.'前';
	return $echo;
}

//把数组变成字符串存储
function array_to_str($arr) {
    $str = '';
    $count = 1;
    foreach ($arr as $a) {
        if ($count==1) {
            $str .= $a;
        } else {
            $str .= '|'.$a;
        }
        $count++;
    }
    return $str;
}

//now url
function en_now_url()
{
	$now_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
	$en_now_url = base64_encode($now_url);
	return $en_now_url;
}

function de_url($url)
{
	$de_now_url = base64_decode($url);
	return $de_now_url;
}

//密码加密函数
function en_password($password)
{
	$b64_pass = base64_encode($password);
	return $b64_pass;
}

